package com.blb.controller;

import com.blb.entity.ProductiveProcess;
import com.blb.entity.page.DataTablePage;
import com.blb.mapper.ProductiveProcessMapper;
import com.blb.service.IProductiveProcessService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("productive")
public class ProductiveProcessController {

    private static final String [] arr = {"","已支付","待发货","已发货","已完成","待排单","待生产","生产中","已完成","已入库","已结束"};
    private static final String [] arr2 = {"","#0f0","#f00","#3A87AD","#ABBAC3","#00D4E5","#919191","#A0A0A0","#82AF6F","#333333","#87B87F"};

    @Autowired
    private IProductiveProcessService productiveProcessService;

    @RequestMapping("")
    @RequiresPermissions("productive_process")
    public String index(){
        return "quality_tracing/productive_process";
    }

    @RequiresPermissions("productive_process")
    @ResponseBody
    @RequestMapping("list")
    public DataTablePage getProductiveByNo(String no, int length, int start){
        String cId = ShiroUtils.getUser().getCompanyId();
        List<Map<String, Object>> list = productiveProcessService.getProductiveByNo(cId,no, length, start*length);
        int count = productiveProcessService.count(cId,no);
        for (int i = 0; i < list.size(); i++ ) {
            //添加状态
            //获取原来的状态数字
            Object orderStatus = list.get(i).get("order_status");
            Object productStatus = list.get(i).get("product_status");
            orderStatus = orderStatus == null ? 3 : orderStatus;
            productStatus = productStatus == null ? 7 : productStatus;
            String newOrderStatus = arr[Integer.parseInt(orderStatus + "")];
            String newProductStatus = arr[Integer.parseInt(productStatus+"")];
            list.get(i).put("order_status",newOrderStatus);
            list.get(i).put("product_status",newProductStatus);
            list.get(i).put("orderColor",arr2[Integer.parseInt(orderStatus + "")]);
            list.get(i).put("productColor",arr2[Integer.parseInt(productStatus+"")]);
        }
        return new DataTablePage(count, count, list);
    }

    @RequiresPermissions("productive_process")
    @ResponseBody
    @RequestMapping("status")
    public ResponseHelp getProductiveProcessByNo(String no){
        List<Map<String, Object>> list = productiveProcessService.getProductiveProcessByNo(no);
        for (int i = 0; i < list.size(); i++ ) {
            list.get(i).put("index",i+1);
            //添加状态
            //获取原来的状态数字
            Object orderStatus = list.get(i).get("status");
            String newOrderStatus = arr[Integer.parseInt(orderStatus + "")];
            list.get(i).put("status",newOrderStatus);
        }
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(list);
        return responseHelp;
    }
}
